package aop;

import com.xiangshi.core.util.DataUtil;
import com.xiangshi.zanbei.api.modules.servicerecord.service.IServiceRecordService;
import com.xiangshi.zanbei.api.modules.user.service.IUserService;
import com.xiangshi.zanbei.cache.GlobalCacheManager;
import com.xiangshi.zanbei.domain.serviceprovider.ServiceProvider;
import com.xiangshi.zanbei.domain.servicerecord.ServiceRecord;
import com.xiangshi.zanbei.domain.user.User;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * Created by LYQ on 2017/12/25.
 */
@Aspect
@Component
public class ServiceProviderAop {

    @Autowired
    private IServiceRecordService serviceRecordService;
    @Autowired
    private IUserService userService;
    @Autowired
    private GlobalCacheManager cacheManager;

    @Pointcut("execution(* com.xiangshi.zanbei.api.modules.serviceprovider.service.impl.ServiceProviderService.insertOne(..))")
    public void insertMethod(){}

    @After("insertMethod()")
    public void afterInsert(JoinPoint joinPoint) throws Throwable{
        Object[] args=joinPoint.getArgs();
        ServiceProvider serviceProvider = (ServiceProvider) args[0];
        //当前操作账户
        Long userid = (Long) args[1];
        if (args.length > 0 && DataUtil.isNotEmpty(serviceProvider) && DataUtil.isNotEmpty(serviceProvider.getId()) && DataUtil.isNotEmpty(serviceProvider.getAppid())){
            //将操作记录添加至ServiceRecord中
            ServiceRecord record = new ServiceRecord();
            record.setAppid(serviceProvider.getAppid());
            record.setUserid(userid);
            record.setService_id(serviceProvider.getId());
            record.setService_name(serviceProvider.getService_name());
            User user = userService.queryById(serviceProvider.getUserid());
            record.setMobile(user.getMobile());
            record.setCurrent_level(serviceProvider.getInitlevel());
            record.setChangetime(new Date());
            record.setLevel_validity(serviceProvider.getLevel_validity());
            record.setTop_service(serviceProvider.getTop_service());
            record.setPoint(serviceProvider.getPoint());
            record.setDescription("添加服务商");
            serviceRecordService.insert(record);
        }
    }

    @Pointcut("execution(* com.xiangshi.zanbei.api.modules.serviceprovider.service.impl.ServiceProviderService.updateOne(..))")
    public void updateMethod(){}

    @AfterReturning(value = "updateMethod()",returning = "result")
    public void afterUpdate(JoinPoint joinPoint, Object result) throws Throwable{
        int str = (int) result;
        Object[] args=joinPoint.getArgs();
        ServiceProvider serviceProvider = (ServiceProvider) args[0];
        String description = "修改服务商信息";
        description = description + "";
        //当前操作账户
        Long userid = (Long) args[1];
        if (str > 0 && DataUtil.isNotEmpty(serviceProvider) && DataUtil.isNotEmpty(serviceProvider.getId()) && DataUtil.isNotEmpty(serviceProvider.getAppid())){
            //从缓存中提取修改前的服务商信息
            ServiceProvider old = cacheManager.getOldSP(serviceProvider.getId());
            if(DataUtil.isEmpty(old)){
                return;
            }
            //将操作记录添加至ServiceRecord中
            ServiceRecord record = new ServiceRecord();
            record.setAppid(serviceProvider.getAppid());
            record.setUserid(userid);
            record.setService_id(serviceProvider.getId());
            record.setService_name(serviceProvider.getService_name());
            User user = userService.queryById(serviceProvider.getUserid());
            record.setMobile(user.getMobile());
            record.setChangetime(new Date());
            //判断是否存在升级级别
            if (DataUtil.isNotEmpty(serviceProvider.getUplevel()) && serviceProvider.getUplevel() != 0) {
                description = description + ",服务商暂时升级";
                record.setCurrent_level(serviceProvider.getUplevel());
                record.setLevel_validity(serviceProvider.getLevel_validity());
            } else {
                description = description + ",服务商永久升级";
                record.setCurrent_level(serviceProvider.getInitlevel());
            }
            if (DataUtil.isNotEmpty(serviceProvider.getTop_service())) {
                description = description + ",修改上级服务商";
                record.setTop_service(serviceProvider.getTop_service());
            }
            if (DataUtil.isNotEmpty(serviceProvider.getPoint()) && !serviceProvider.getPoint().equals(old.getPoint())) {
                description = description + ",修改服务商点数";
                record.setPoint(serviceProvider.getPoint());
            }
            //判断原等级
            if (!DataUtil.isEmpty(old.getUplevel())){
                Date now = new Date();
                Date va = old.getLevel_validity();
                //现在时间大于到期时间
                if (va.getTime() <= now.getTime()){
                    record.setOld_level(old.getInitlevel());
                }else {
                    record.setOld_level(old.getUplevel());
                }
            }else {
                record.setOld_level(old.getInitlevel());
            }
            record.setDescription(description);
            serviceRecordService.insert(record);
        }
    }

}
